/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.TX.tta;

import Helper.TTAHelper.Helper_CustomerLoyalty;
import Helper.TTAHelper.Helper_CustomerValue;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import managedbean.TX.CustomerIOTRPortal.FakeMainPortalMB;
import managedbean.TX.OutletManagement.TrackerManagedOutletMB;
import session.TX.CustomerAnalysisSBRemote;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "custAnalysisMB")
@ViewScoped
public class OutletCustAnalysisMB {

    @EJB
    CustomerAnalysisSBRemote analysisBean;
    @ManagedProperty(value = "#{fakeMainPortalMB}")
    private FakeMainPortalMB portalManagedBean;
    @ManagedProperty(value = "#{TrackerOutletAdmin}")
    private TrackerManagedOutletMB outletTrackBean;
    private Double discountRate = new Double(0.15);
    private Double expPerRes = new Double(100);
    private Double avgExp_CustValue = new Double(100);

    /** Creates a new instance of OutletCustAnalysisMB */
    public OutletCustAnalysisMB() {
    }

    public Double getCLV() {
        System.out.println("im here");
        String stakeholderName = portalManagedBean.getStakeholderName();
        if (stakeholderName == null) {
            return null;
        } else {
            if (discountRate != null && expPerRes != null) {
                return analysisBean.calculateGC(stakeholderName, discountRate, expPerRes);
            } else {
                return null;
            }
        }
    }
    
    public void updateCLVParameter(ActionEvent event){
    }
    
    public List<Helper_CustomerLoyalty> getSortedLocalLoyaltyIdx() {
        Long outletId = outletTrackBean.getOutletId();
        if (outletId == null) {
            return null;
        }
        return analysisBean.getSortedLocalLoyaltyIdx(outletId);
    }

    public List<Helper_CustomerLoyalty> getSortedForeignLoyaltyIdx() {
        Long outletId = outletTrackBean.getOutletId();
        if (outletId == null) {
            return null;
        }
        return analysisBean.getSortedForeignLoyaltyIdx(outletId);
    }
    
    public List<Helper_CustomerValue> getSortedLocalCustValue(){
        Long outletId = outletTrackBean.getOutletId();
        if (outletId == null) {
            return null;
        }
        List<Helper_CustomerValue> returnList = analysisBean.getSortedLocalCustValue(outletId, avgExp_CustValue);
        for(int i = 0 ; i < returnList.size(); i++) {
            returnList.get(i).setRank(i+1);
        }
        return returnList;
    }
    
    public List<Helper_CustomerValue> getSortedForeignCustValue(){
        Long outletId = outletTrackBean.getOutletId();
        if (outletId == null) {
            return null;
        }
        List<Helper_CustomerValue> returnList = analysisBean.getSortedForeignCustValue(outletId, avgExp_CustValue);
        for(int i = 0 ; i < returnList.size(); i++) {
            returnList.get(i).setRank(i+1);
        }
        return returnList;
    }

    public FakeMainPortalMB getPortalManagedBean() {
        return portalManagedBean;
    }

    public void setPortalManagedBean(FakeMainPortalMB portalManagedBean) {
        this.portalManagedBean = portalManagedBean;
    }

    public Double getDiscountRate() {
        return discountRate;
    }

    public void setDiscountRate(Double discountRate) {
        this.discountRate = discountRate;
    }

    public Double getExpPerRes() {
        return expPerRes;
    }

    public void setExpPerRes(Double expPerRes) {
        this.expPerRes = expPerRes;
    }

    public TrackerManagedOutletMB getOutletTrackBean() {
        return outletTrackBean;
    }

    public void setOutletTrackBean(TrackerManagedOutletMB outletTrackBean) {
        this.outletTrackBean = outletTrackBean;
    }

    public Double getAvgExp_CustValue() {
        return avgExp_CustValue;
    }

    public void setAvgExp_CustValue(Double avgExp_CustValue) {
        this.avgExp_CustValue = avgExp_CustValue;
    }
    
}
